﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waiter.Controler;
using Waiter.Common;
using Waiter.View;

namespace Waiter.CustomControl
{
    public partial class TableItem : UserControl
    {
        public TableItem()
        {
            InitializeComponent();
        }
        public TableItem(int x, int y)
        {
            InitializeComponent();
            this.BackColor = System.Drawing.SystemColors.Control;
            this.Location = new Point(x, y);
            this.Name = "";
        }

        private string tableCode = "";
        private int tableStatus = (int)TableStatus.NOT_USE;
        private string oldBill = null;

        public string TableCode
        {
            get { return tableCode; }
            set { tableCode = value; }
        }

        public void fsLoadTableItem(TABLE tableItem)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "TableItem.fsLoadTableItem", "", "");

            this.tableCode = tableItem.CODE;
            this.tableStatus = tableItem.STATUS_CODE;
            this.lblTable.Text = tableItem.TABLE_NAME;

            // set table item info
            fsUpdateStatus(tableItem.STATUS_CODE, CommonConstants.FLAG_OFF);
        }

        private void lblTable_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "TableItem.lblTable_Click", "", "");

            CommonGlobalVariable.currentTable = null;
            if (this.tableStatus != (int) TableStatus.BUSY)
            {
                this.ctmOrder_Click(null, null);
                return;
            }

            FrmCouponDetials frm = new FrmCouponDetials(tableCode);
            if (frm.CanShowFlag == CommonConstants.FLAG_OFF)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Không tìm thấy thông tin gọi món!!!", 
                    CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                fsUpdateStatus((int) TableStatus.EMPTY, CommonConstants.FLAG_ON);
                return;
            }
            frm.StartPosition = FormStartPosition.Manual;
            frm.Location = new Point(Cursor.Position.X, Cursor.Position.Y);
            frm.ShowDialog();
        }

        public void fsRefreshStatus()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "TableItem.fsRefreshStatus", "", "");

            TableCtrl tableCtrl = new TableCtrl();
            TABLE item = tableCtrl.fsFindTableByCode(this.tableCode);
            fsUpdateStatus(item.STATUS_CODE, CommonConstants.FLAG_OFF);
        }

        private void fsUpdateStatus(int status, bool dbFlag)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "TableItem.fsUpdateStatus", "", "");

            if (dbFlag == CommonConstants.FLAG_ON)
            {
                TableCtrl tableCtrl = new TableCtrl();
                if (tableCtrl.fsUpdateTableStatus(this.tableCode, status) != ResultStatus.SUCCESS)
                {
                    return;
                }
            }

            this.tableStatus = status;
            switch (status)
            {
                case (int) TableStatus.BUSY:
                    this.lblTable.Style.BackgroundImage = global::Waiter.Properties.Resources.Person_Coffee;
                    this.lblTable.Style.ForeColor.ColorSchemePart = DevComponents.DotNetBar.eColorSchemePart.ItemText;
                    break;
                case (int) TableStatus.EMPTY:
                    this.lblTable.Style.BackgroundImage = null;
                    this.lblTable.Style.ForeColor.Color = Color.Gray;
                    break;
                case (int) TableStatus.MERGE:
                    AreaItem area1 = (AreaItem)this.Parent;
                    area1.Controls.Remove(this);
                    area1.fsRefreshAreaItem();
                    break;
                case (int) TableStatus.NOT_USE:
                    AreaItem area2 = (AreaItem)this.Parent;
                    area2.Controls.Remove(this);
                    area2.fsRefreshAreaItem();
                    break;
            }
        }

        private void ctmOrder_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "TableItem.ctmOrder_Click", "", "");

            FrmOrderCoupon frm = new FrmOrderCoupon(this.tableCode);
            frm.ShowDialog();
            if (frm.DataChangedFlag == CommonConstants.FLAG_ON)
            {
                TableCtrl tableCtrl = new TableCtrl();
                TABLE table = tableCtrl.fsFindTableByCode(this.tableCode);
                fsUpdateStatus(table.STATUS_CODE, CommonConstants.FLAG_OFF);

                this.oldBill = null;
            }
        }

        private void ctmTable_Opening(object sender, CancelEventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "TableItem.ctmTable_Opening", "", "");

            CommonGlobalVariable.currentTable = null;
            if (this.tableStatus == (int) TableStatus.EMPTY)
            {
                this.ctmCalculateBill.Visible = false;
                this.ctmChangeTable.Visible = false;
                bool reCalBill = (this.oldBill != null);
                this.ctmReCalculateBill.Visible = reCalBill;
                this.toolStripSeparator1.Visible = reCalBill;
                this.ctmDeleteAll.Visible = false;
            }
            else if (this.tableStatus == (int)TableStatus.BUSY)
            {
                this.ctmCalculateBill.Visible = true;
                this.ctmChangeTable.Visible = true;
                this.ctmReCalculateBill.Visible = false;
                this.toolStripSeparator1.Visible = true;
                this.ctmReCalculateBill.Visible = false;
                this.ctmDeleteAll.Visible = true;
            }
        }

        private void ctmCalculateBill_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "TableItem.ctmCalculateBill_Click", "", "");

            FrmCaculateBill frm = new FrmCaculateBill(this.tableCode);
            frm.ShowDialog();
            if (frm.DataChangeFlag)
            {
                fsUpdateStatus((int) TableStatus.EMPTY, CommonConstants.FLAG_ON);
                this.oldBill = frm.BillItem.CODE;
            }
        }

        private void ctmChangeTable_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "TableItem.ctmChangeTable_Click", "", "");

            FrmOrderChange frm = new FrmOrderChange(this.tableCode);
            frm.ShowDialog();
            if (frm.DataChangedFlag)
            {
                FrmSales item = (FrmSales) this.ParentForm;
                item.fsRefreshStatus();
            }
        }

        private void ctmReCalculateBill_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "TableItem.ctmReCalculateBill_Click", "", "");

            FrmInputText frmInput = new FrmInputText();
            frmInput.fsSetDialogInfo("Lý do hủy hóa đơn", true);
            if (frmInput.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            string note = frmInput.fsGetInputText();

            string tempBill = oldBill;

            // create new bill
            this.ctmOrder_Click(null, null);
            if (this.oldBill == null)
            {
                this.ctmCalculateBill_Click(null, null);
                if (this.oldBill != null)
                {
                    // destroy old bill
                    BillCtrl billCtrl = new BillCtrl();
                    if (billCtrl.fsUpdateStatus(tempBill, (int)BillStatus.NOT_USE, note) != ResultStatus.SUCCESS)
                    {
                        DevComponents.DotNetBar.MessageBoxEx.Show("Đã có lổi xảy ra, vui lòng thực hiện lại",
                            CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
            }
        }

        private void lblTable_MouseEnter(object sender, EventArgs e)
        {
            if (CommonGlobalVariable.currentTable != null)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "TableItem.lblTable_MouseEnter", "", "");

                string tableFrom = CommonGlobalVariable.currentTable;
                CommonGlobalVariable.currentTable = null;

                TableCtrl tableCtrl = new TableCtrl();
                TABLE itemFrom = tableCtrl.fsFindTableByCode(tableFrom);
                TABLE itemTo = tableCtrl.fsFindTableByCode(this.tableCode);
                DialogResult confirm = DevComponents.DotNetBar.MessageBoxEx.Show("Bạn có chắc muốn chuyển tất cả các món từ bàn " 
                    + itemFrom.TABLE_NAME + " sang bàn "+ itemTo.TABLE_NAME + " không ?", 
                    CommonMessages.MSG_CONFIRM_TITLE, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (confirm == DialogResult.No)
                {
                    return;
                }

                OrderCouponCtrl orderCouponCtrl = new OrderCouponCtrl();
                if (orderCouponCtrl.fsChangeAllOrderCoupon(itemFrom.CODE, itemTo.CODE) == ResultStatus.SUCCESS)
                {
                    FrmSales item = (FrmSales)this.ParentForm;
                    item.fsRefreshStatus();
                }
                else
                {
                    DevComponents.DotNetBar.MessageBoxEx.Show("Đã có lổi xảy ra, vui lòng thực hiện lại!", 
                        CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void lblTable_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.tableStatus != (int) TableStatus.BUSY)
            {
                return;
            }

            Cursor = new Cursor(Properties.Resources.Martin_Berube_Food_Coffee.Handle);
            CommonGlobalVariable.currentTable = this.tableCode;
        }

        private void lblTable_MouseUp(object sender, MouseEventArgs e)
        {
            Cursor = Cursors.Default;
        }

        private void ctmDeleteAll_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "TableItem.ctmDeleteAll_Click", "", "");

            DialogResult dlr = DevComponents.DotNetBar.MessageBoxEx.Show("Bạn có chắc muốn hủy các món đã gọi của bàn " + this.lblTable.Text + " không?", CommonMessages.MSG_CONFIRM_TITLE, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dlr != DialogResult.Yes)
            {
                return;
            }

            OrderCouponCtrl orderCouponCtrl = new OrderCouponCtrl();
            ResultStatus rs = orderCouponCtrl.fsHRemoveCurrentOrderCoupon(this.tableCode);
            if (rs != ResultStatus.SUCCESS)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Đã có lổi xảy ra, vui lòng thực hiện lại!",
                        CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            this.tableStatus = (int)TableStatus.EMPTY;
            fsUpdateStatus(this.tableStatus, true);
        }
    }
}